//*************************************************************************\\
//***********               tdc_automata_engine.cpp    		***********\\
//*************************************************************************\\
//*************************************************************************\\
//*********** 		        UFPE/Cin - Greco		***********\\
//*********** 		 http://www.cin.ufpe.br/~greco 		***********\\
//***********							***********\\
//***********							***********\\
//*********** 		    By Rafael Melo Macieira		***********\\
//***********							***********\\
//***********		    E-mail: rmm2@cin.ufpe.br		***********\\
//*************************************************************************\\
//*************************************************************************\\
//**		 Automata Reference for the temporal check		 **\\
//**	   	 Automatas Configuration Class                           **\\
//*************************************************************************\\

#include "headers/tdc_automata_engine.h"
#include <stdio.h>
#include "headers/address.h"

TDC_Automata_Engine::TDC_Automata_Engine(){
	unsigned int* var1;
    //var1 = 1;
    //char* var2 = "b";
    //printf("\nVar1: %p - var2: %p\n",var1,var2);
    VarList *vl1= new VarList(LCD_CTRL_REG_ADDR, var1);
    //VarList *vl2 = new VarList(LCD_VAR_CMD_ADDR, var2);
    TDC_Automata *aut1 = new TDC_Automata_drv_init(vl1);
    TDC_Automata *aut2 = new TDC_Automata_drv_send_data_pre(vl1);
    TDC_Automata *aut3 = new TDC_Automata_drv_setcontrast_pre(vl1);
    AutomataList *al1 = new AutomataList(FUNC_INIT_ADDR,aut1);
    AutomataList *al2 = new AutomataList(FUNC_SEND_DATA_ADDR,aut2);
    AutomataList *al3 = new AutomataList(FUNC_SET_CONTRAST_ADDR,aut3);
    
    //vl1->addNextVar(vl2);
    al1->addNextAutomata(al2);
    al2->addNextAutomata(al3);

    this->runStepNow = 0;
    //this->currentAutomata = new TDC_Automata_drv_init(vl1);
    this->mainVarList = vl1;
    this->mainAutomataList = al1;
    //printf("End: %p",this->mainVarList);
};

void TDC_Automata_Engine::checkThisVar(unsigned int platformAddr){
    this->runStepNow = 0;
    //printf("This: %p",this);
    //printf("Endereço: %d", platformAddr);
    //printf("End2: %p",this->mainVarList)
    bool ret = this->mainVarList->contains(this->mainVarList, platformAddr);
    //printf("Tem?: %i",ret);
    if (ret) {
      //printf("TEM!!!");
  	//printf("\n - DADO");
        this->runStepNow = 1;
        this->currentVarList = mainVarList->getVarListByPlatformAddr(mainVarList,platformAddr);
        //printf("\nCurrentVarList: %p\n",currentVarList);
    }else{
    	ret = this->mainAutomataList->contains(this->mainAutomataList, platformAddr);
        if (ret) {
		//printf("\nFUNCAO");
		this->currentAutomata = mainAutomataList->getAutomataListByPlatformAddr(mainAutomataList,platformAddr)->getAutomata();
                this->currentAutomata->reset();
        }
    }
    
};
void TDC_Automata_Engine::runStep(bool access_type, unsigned int data){
    //printf("Run now?: %d",this->runStepNow);
    if (this->runStepNow) {
        //printf("Aha!!: %s\n",this->currentAutomata->name);
        this->currentVarList->setVar(&data);
        //char* tmpVar = currentVarList->getVar();
        //*tmpVar = *data;

        this->currentAutomata->runStep(access_type, this->mainVarList); //MUDAR PARA ATUALIZAR SOMENTE UMA VARIAVEL!!! BY ADDR
    }
};
int TDC_Automata_Engine::isVarToCheck(unsigned int platformAddr){

};
